package com.huawei123.hasee.cniao5store.mvp.interactor;

import com.huawei123.hasee.cniao5store.api.IGetDataDelegate;
import com.huawei123.hasee.cniao5store.api.RecommendApi;
import com.huawei123.hasee.cniao5store.api.TopApi;
import com.huawei123.hasee.cniao5store.base.BaseActivity;
import com.huawei123.hasee.cniao5store.bean.TopBean;
import com.huawei123.hasee.cniao5store.utils.JsonParseUtils;
import com.zhxu.library.http.HttpManager;
import com.zhxu.library.listener.HttpOnNextListener;

import javax.inject.Inject;

/**
 * 项目名称：Cniao5Store
 * 类描述：
 * 创建人：luohao
 * 创建时间：2018/1/31 15:47
 * 修改人：luohao
 * 修改时间：2018/1/31 15:47
 * 修改备注：
 */

public class TopInteractor {

    private IGetDataDelegate<TopBean> delegate;

    @Inject
    public TopInteractor() {

    }


    public void loadTopData(BaseActivity activity, IGetDataDelegate<TopBean> delegate) {
        this.delegate = delegate;
        TopApi topApi = new TopApi(listener, activity);
        HttpManager manager = HttpManager.getInstance();
        manager.doHttpDeal(topApi);
    }

    HttpOnNextListener listener = new HttpOnNextListener<TopBean>() {

        @Override
        public void onNext(TopBean topBean) {
            delegate.getDataSuccess(topBean);
        }

        @Override
        public void onCacheNext(String string) {
            super.onCacheNext(string);
            TopBean topBean = JsonParseUtils.parseTopBean(string);
            delegate.getDataSuccess(topBean);
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            delegate.getDataError(e.getMessage());
        }
    };
}
